# asterisk-zaptrunks.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

# This file is part of the CarrierClass.net project.
# For more information: http://carrierclass.net/
#
package CCNv2::Provision::Asterisk::zaptrunks;
use strict;
use warnings;
use base qw(CCNCore::Provision::Base);
use CCN::Text;
use CCNCore::Scripting;

sub run()
{
    my $self = shift;
    return unless defined $self->p->{cp_member};

    my $zapataconf = '';
    my $zaptelconf = '';
    my $zaprouteconf = '';


    $self->sql_do_for( <<'SQL', $self->p->{cp_member},
        SELECT pk, zap_prigroup, zap_rxgain, zap_txgain, zap_switchtype, zap_signalling, zap_span, zap_framing, zap_encoding, zap_crc, zap_clock, zap_lbo, zap_channel_start, zap_channel_end, zap_d_channel, zap_tonezone
        FROM trunk WHERE type = 'zap' AND server = ?;
SQL
    sub
    {
        my ($TRUNK,$zap_prigroup,$zap_rxgain,$zap_txgain,$zap_switchtype,$zap_signalling,$zap_span,$zap_framing,$zap_encoding,$zap_crc,$zap_clock,$zap_lbo,$zap_channel_start,$zap_channel_end,$zap_d_channel,$zap_tonezone) = @_;
        # Inbound routing
        $zaprouteconf .= $self->ast_route_trunk_inbound(${TRUNK});

        # Outbound routing.
        $zaprouteconf .= $self->ast_route_trunk_outbound(${TRUNK});
        $zaprouteconf .= 
            "[dial-trunk-$TRUNK]\n".
            "exten => _X.,1,Dial(Zap/${zap_span}/\${EXTEN})\n";

        # ZAP-specific parameters

        my $bchannel = "${zap_channel_start}-${zap_channel_end}";
        # See http://www.voip-info.org/wiki/view/Zaptel.conf+span+syntax
        my $timing;
        foreach ($zap_clock)
        {
            $timing = 0 if /^internal$/;
            $timing = 1 if /^primary$/;
            $timing = 2 if /^secondary$/;
        }
        my $lbo;
        foreach ($zap_lbo)
        {
            $lbo = 0 if $_ == 0 # CSU
                     or ($_ >=  0 && $_ <= 133); # DSX-1
            $lbo = 1 if $_ >= 133 && $_ <= 266; # DSX-1
            $lbo = 2 if $_ >= 266 && $_ <= 399; # DSX-1
            $lbo = 3 if $_ >= 399 && $_ <= 533; # DSX-1
            $lbo = 4 if $_ >= 533 && $_ <= 655; # DSX-1
            $lbo = 5 if $_ == -7.5;
            $lbo = 6 if $_ == -15;
            $lbo = 7 if $_ == -22.5;
            $self->warn ("Invalid Line Build-Out value $_ for ${TRUNK}")
                if not defined $lbo;
        }
        my $coding = $zap_encoding;
        $coding .= ',crc' if defined $zap_crc and $zap_crc =~ /^t/i;

        $zapataconf .= "context = from-gateway-${TRUNK}\n";
        $zapataconf .= "group $zap_prigroup\n" if defined $zap_prigroup;
        $zapataconf .= "txgain $zap_txgain\n" if defined $zap_txgain;
        $zapataconf .= "txgain $zap_rxgain\n" if defined $zap_rxgain;
        $zapataconf .= "channel => $bchannel\n";

        $zaptelconf .= <<"EOT";
loadzone=${zap_tonezone}
defaultzone=${zap_tonezone}
span=$zap_span,$timing,$lbo,$zap_framing,$coding
bchan=$bchannel
dchan=$zap_d_channel
EOT

    }); # foreach @zaptrunks

    foreach('/etc/zaptel.conf')
    {
        CAT $_, copyright(';') . $zaptelconf;
        # NOTE: Should have "fxotune -s" if using Digium analog cards.
        CCNv2::Scripting::IF_CHANGED_restart_asterisk($_);
    }

    foreach('/etc/asterisk/zapata.conf')
    {
        CAT $_, copyright(';') . <<'EOT' . $zapataconf;
[trunkgroups]
; Trunk groups are used for NFAS or GR-303 connections.

[channels]
language=en

useincomingcalleridonzaptransfer=yes
callerid=asreceived
immediate=no

echocancel=yes
echocancelwhenbridged=no
echotraining=no

resetinterval=never

EOT

        CCNv2::Scripting::IF_CHANGED_restart_asterisk($_);
    }

    foreach('/etc/asterisk/config/extensions.zaptrunks.conf')
    {
        CAT $_, $zaprouteconf;
        IF_CHANGED $_ => CCNv2::Scripting::AST('extensions reload');
    }
}
1;